*Preparing Imputation files
*12-06-24_prep_mul-imputation.do

clear

*global filetree /Users/Allan/Dropbox/!!Papers/Liberal Peace/12-02-21_ISQ_commentary/Mousseau_Replication_Files/

cd "$filetree"

set more off

**Importing dyad-year dataset from EUGene
do "12-05-28_dyad.out.do"

order ccode1 ccode2 year abbrev1 abbrev2 cwmid cwongo cwfatald polity21 polity22

*Dyadic Democracy Variables
g dml=min(polity21, polity22) if polity21~=. & polity22~=.
label variable dml "DemocracyLow"
g dmh=max(polity21, polity22) if polity21~=. & polity22~=.
label variable dmh "DemocracyHigh"

g mjpw=max(majpow1, majpow2) if majpow1~=. & majpow2~=.
label variable mjpw "Major power"


*contigl defined as contiguity by land
g contigl=0 if contig~=1 & contig~=.
replace contigl=1 if contig==1
label variable contigl "Land Contiguity"


gen lndist=log(distance+1)

g cpl = cap_1 if cap_1 < cap_2 & cap_1~=. & cap_2~=.
replace cpl = cap_2 if cap_2 <= cap_1 & cap_1~=. & cap_2~=.
g cph = cap_1 if cap_1 > cap_2 & cap_1~=. & cap_2~=.
replace cph = cap_2 if cap_2 >= cap_1 & cap_1~=. & cap_2~=.
g lncprt=ln(cph/cpl) if cph~=. & cpl~=.
*drop cap_1 cap_2 cpl cph
label variable lncprt "Relative capability"

*calculate probability of winning using CINCs*
gen pwin_cap=cph/(cph+cpl)

gen dyadid=(ccode1*1000)+ccode2

sort ccode1 ccode2 year
order ccode1 ccode2 year abbrev1 abbrev2 cwmid cwongo cwfatald dyadid pwin_cap lncprt mjpw dml dmh contigl lndist


**Dependent Variables
sort dyadid year
sort dyadid
by dyadid: gen midonsl=cwmid[_n+1] 
by dyadid: replace midonsl=. if cwongo[_n+1]==1 & cwmid[_n+1]==0 
label variable midonsl "MID onset lead"

sort dyadid year
sort dyadid
by dyadid: gen midongl=cwongo[_n+1] 
by dyadid: replace midongl=1 if cwmid[_n+1]==1
label variable midongl "MID ongoing lead"
*defining ongoing as onset or ongoing

sort dyadid year
sort dyadid
by dyadid: gen fmidonsl=1 if midonsl==1 & cwfatald[_n+1]>0 & cwfatald[_n+1]~=.
by dyadid: replace fmidonsl=0 if midonsl==0 | cwfatald[_n+1]==0 
by dyadid: replace fmidonsl=. if fmidonsl[_n+1]==0 & cwfatald[_n+1]>0 
label variable fmidons "Fatal MID onset lead"

sort dyadid year
sort dyadid
by dyadid: gen fmidongl=1 if midongl==1 & cwfatald[_n+1]>0 & cwfatald[_n+1]~=.
by dyadid: replace fmidongl=0 if midongl==0 | cwfatald[_n+1]==0 
label variable fmidongl "Fatal MID ongoing lead"

sort dyadid year
sort dyadid
by dyadid: gen warl=1 if midongl==1 & cwfatald[_n+1]==6
by dyadid: replace warl=0 if midongl==0 | cwfatald[_n+1]<6 
label variable warl "War ongoing lead"

 tab midonsl midongl, m
  tab fmidonsl fmidongl, m
  tab warl fmidongl, m
  
  
  
***Peace-years

drop if year<1950
drop if year>2001

btscs midongl year dyadid, g(midyears)
gen midyears2=midyears^2
gen midyears3=midyears^3

btscs fmidongl year dyadid, g(fmidyears)
gen fmidyears2=fmidyears^2
gen fmidyears3=fmidyears^3

btscs warl year dyad, g(waryears)
gen waryears2=waryears^2
gen waryears3=waryears^3


*drop country_name1 country_name2
kountry ccode1, from(cown)
rename NAMES_STD country_name1
 
kountry ccode2, from(cown) 
rename NAMES_STD country_name2


order country_name*
save "12-05-28_dem_CIE_analysis.dta", replace

**Produce "country_year_mimpute.dta"
do "12-06-11_country_year.do"


**See R code "MI_Dem_CIE.r" for multiple imputation***
*AD: I opted to use Amelia II because it had a convenient technique for having within country temporal trends.
*R code creates files: "outdatatotal1.dta" and other files with increasing numbers for each imputation
***

